//
//  TabBar.m
//  YSSmartHome
//
//  Created by Yahaong on 16/8/10.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "TabBar.h"
#import "UIView+Frame.h"

@interface TabBar ()
@property(nonatomic, strong)UIButton *voiceBtn;
@end
@implementation TabBar

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIButton *voiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        voiceBtn.adjustsImageWhenDisabled = false;
        [voiceBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_voice"] forState:UIControlStateNormal];
    
        [voiceBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        voiceBtn.yh_size = voiceBtn.currentBackgroundImage.size;
        [voiceBtn addTarget:self action:@selector(voiceClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:voiceBtn];
        self.voiceBtn = voiceBtn;
    }
    return self;
}

-(void)layoutSubviews
{
    //不能删，一定要调用，等布局完我们再覆盖行为
    [super layoutSubviews];
    //覆盖排布
    //1.设置自定义按钮的位置
    self.voiceBtn.yh_centerX = self.yh_width*0.5;
    self.voiceBtn.yh_y = -15;
    //2.设置其他tabbarButton的位置和尺寸
    CGFloat tabbarButtonW = self.yh_width/5;
    CGFloat tabbarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        Class class =  NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
            //设置宽度
            child.yh_width = tabbarButtonW;
            //设置x
            child.yh_x = tabbarButtonIndex*tabbarButtonW;
            //增加索引
            tabbarButtonIndex++;
            if (tabbarButtonIndex == 2) {
                tabbarButtonIndex++;
            }
        }
    }
}

/*
 *  语音点击事件
 */
-(void)voiceClick
{
    //通知代理
    if ([self.tabBardelegate respondsToSelector:@selector(tabBarDidClickVoiceButton:)]) {
        [self.tabBardelegate tabBarDidClickVoiceButton:self];
    }
}

//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
//{
//    CGPoint newPoint = [self convertPoint:point toView:self.voiceBtn];
//    if ([self.voiceBtn pointInside:newPoint withEvent:event]) {
//        return self.voiceBtn;
//    }else
//    {
//        return [super hitTest:point withEvent:event];
//    }
//}

- (void)setButtonEnable:(BOOL)buttonEnable
{
    _buttonEnable = buttonEnable;
    self.voiceBtn.enabled = buttonEnable;
}
@end
